using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class RaceEditorMenu : MonoBehaviour {
	
	public UILabel raceName;
	public UILabel speedLabel;
	public UIPopupList sizePopup;
	public UICheckbox quadrupedCheckBox;
	public UILabel strengthMod;
	public UILabel dexterityMod;
	public UILabel constitutionMod;
	public UILabel intelligenceMod;
	public UILabel wisdomMod;
	public UILabel charismaMod;

	public GameObject AddAbilityButton;
	public GameObject CancelButton;
	public GameObject AcceptButton;
	
	List<GameObject> abilityLabels;
	
	List<string> raceAbilities;
	
	public UITable holder;
	
	public GameObject target;
	public string functionName;
	
	string origName;
	
	static GameReference _reference;
	
	static GameReference reference
	{
		get
		{
			if(_reference == null)
				_reference = GameObject.FindObjectOfType(typeof(GameReference)) as GameReference;
			return _reference;
		}
	}
	
	bool Loaded = false;

	// Use this for initialization
	void Start () {	
			UIButtonMessage message = AddAbilityButton.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "AddAbilityClicked";
		
		if(!Loaded)
		{			
			message = CancelButton.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "CancelButtonClicked";
			
			message = AcceptButton.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "AcceptButtonClicked";
			
			abilityLabels = new List<GameObject>();
			raceAbilities = new List<string>();
		}
	}
	
	public void LoadRaceDisplay(string rName, bool noEdit = false)
	{
		abilityLabels = new List<GameObject>();
		raceAbilities = new List<string>();
		
		Race r = RaceManager.Load(rName);
		
		origName = rName;
		
		raceName.text = r.name;
		speedLabel.text = r.Speed.ToString();
		sizePopup.selection = r.Size;
		
		strengthMod.text = r.STRMod.ToString();
		dexterityMod.text = r.DEXMod.ToString();
		constitutionMod.text = r.CONMod.ToString();
		intelligenceMod.text = r.INTMod.ToString();
		wisdomMod.text = r.WISMod.ToString();
		charismaMod.text = r.CHAMod.ToString();
		
		quadrupedCheckBox.isChecked = r.Quadruped;
		
		Loaded = true;
		
		CreateAbilityObjects(r.racialAbilities);
		
		if(noEdit)//!isCustom && Application.platform != RuntimePlatform.WindowsEditor)
		{
			NGUITools.AddWidgetCollider(gameObject);
			
			foreach(Collider l in gameObject.GetComponentsInChildren<Collider>())
			{
				l.enabled = false;
			}
			
			collider.enabled = true;
			
			AddAbilityButton.SetActiveRecursively(false);
			CancelButton.SetActiveRecursively(false);
			
			UIButtonMessage message = AcceptButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "CancelButtonClicked";
			message.collider.enabled = true;
		}
		else
		{
			foreach(UIInput l in gameObject.GetComponentsInChildren<UIInput>())
			{
				l.text = l.label.text;
				l.mDefaultText = "Some Info";
			}
			
			UIButtonMessage message = AcceptButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "SaveChangesClicked";
			
			CancelButton.GetComponentInChildren<UILabel>().text = "Delete!";
			message = CancelButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "DeleteClicked";
		}
	}

	
	void CreateAbilityObjects(List<string> selections)
	{
		if(selections != null)
			raceAbilities = selections;
		
		foreach(GameObject ability in abilityLabels)
		{
			Destroy(ability);
		}
		
		raceAbilities.Sort();
		for(int i = 0; i < raceAbilities.Count; i++)
		{
			GameObject g = Instantiate(reference.SkillLabelPrefab) as GameObject;
			g.name = "l " + (i+2) + " AbilityLabel";
			g.transform.parent = AddAbilityButton.transform.parent;
			g.active = true;
			g.transform.localScale = Vector3.one;
			g.transform.position = AddAbilityButton.transform.position;
			
			g.GetComponentInChildren<UIDragObject>().target = holder.transform;
			
			g.GetComponentInChildren<UILabel>().text = raceAbilities[i];
			abilityLabels.Add(g);
		}
		
		holder.repositionNow = true;
	}
	
	void AddAbilityClicked()
	{
		BrowserManager.CreateAbilityBrowser(gameObject,"CreateAbilityObjects",raceAbilities, true, true);
	}
	
	void SaveChangesClicked()
	{
		Race r = new Race();
		
		r.name = raceName.text;
		r.Speed = int.Parse(speedLabel.text);
		r.Size = sizePopup.selection;
		
		r.STRMod = int.Parse(strengthMod.text);
		r.DEXMod = int.Parse(dexterityMod.text);
		r.CONMod = int.Parse(constitutionMod.text);
		r.INTMod = int.Parse(intelligenceMod.text);
		r.WISMod = int.Parse(wisdomMod.text);
		r.CHAMod = int.Parse(charismaMod.text);
		
		r.Quadruped = quadrupedCheckBox.isChecked;
		
		r.racialAbilities = raceAbilities;
		
		if(RaceManager.SaveRace(r, origName))
		{
			target.SendMessage("renamedObject", new KeyValuePair<string, string>(origName, r.name), SendMessageOptions.DontRequireReceiver);
			
			Destroy(gameObject);
		}
	}
	
	void CancelButtonClicked()
	{
		Destroy(gameObject);
	}
	
	IEnumerator DeleteClicked()
	{
		List<bool> b = new List<bool>{false};
		
		yield return StartCoroutine(HelperFunctions.ConfirmationWindow("Are you sure you want to delete " + origName + "?", b));
		
		if(b[0])
		{
			RaceManager.DeleteRace(origName);
			
			if(target != null)
				target.SendMessage(functionName);
		}

		Destroy(gameObject);
	}
	
	void AcceptButtonClicked()
	{
		Race r = new Race();
		
		r.name = raceName.text;
		r.Speed = int.Parse(speedLabel.text);
		r.Size = sizePopup.selection;
		
		r.STRMod = int.Parse(strengthMod.text);
		r.DEXMod = int.Parse(dexterityMod.text);
		r.CONMod = int.Parse(constitutionMod.text);
		r.INTMod = int.Parse(intelligenceMod.text);
		r.WISMod = int.Parse(wisdomMod.text);
		r.CHAMod = int.Parse(charismaMod.text);
		
		r.Quadruped = quadrupedCheckBox.isChecked;
		
		r.racialAbilities = raceAbilities;
		
		r = RaceManager.AddRace(r.name, r.description, r.Size, r.racialAbilities, r.STRMod, r.DEXMod, r.CONMod, r.INTMod, r.WISMod, r.CHAMod, r.Speed, r.Quadruped);
		
		if(target != null)
			target.SendMessage(functionName, new KeyValuePair<string,string>(r.name, r.description), SendMessageOptions.DontRequireReceiver);
		
		Destroy(gameObject);
	}
}
